package cn.blnp.net.flowable.boot.localize.flow.recall;

import cn.blnp.net.flowable.boot.service.RevokeProcessInstanceService;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.filter.SimplePropertyPreFilter;
import lombok.extern.slf4j.Slf4j;
import org.flowable.common.engine.impl.identity.Authentication;
import org.flowable.engine.*;
import org.flowable.engine.repository.Deployment;
import org.flowable.engine.repository.ProcessDefinition;
import org.flowable.engine.runtime.ExecutionQuery;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.task.api.Task;
import org.flowable.task.api.TaskQuery;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import javax.annotation.Resource;
import java.util.List;

/**
 * <h3>流程撤销单元测试用例</h3>
 *
 * @author <a href="mailto:blnp.yibin@qq.com">lyb</a>
 * @version 1.0
 * @since 2025/6/19 10:11
 */
@Slf4j
@SpringBootTest
public class FlowRecallTest {

    @Resource
    private RepositoryService repositoryService;
    @Resource
    private RuntimeService runtimeService;
    @Resource
    private IdentityService identityService;
    @Resource
    private TaskService taskService;
    @Resource
    private HistoryService historyService;
    @Resource
    private ManagementService managementService;
    @Resource
    private RevokeProcessInstanceService revokeProcessService;

    SimplePropertyPreFilter taskFilter = new SimplePropertyPreFilter(Task.class, "id",
            "name", "processInstanceId", "taskDefinitionKey");

    /**
     *  <h1>流程撤销案例演示</h1>
     *  <ul>
     *      <li><b>流程定义文件：</b>{@link flow/revokeProcessInstanceProcess.bpmn20.xml}</li>
     *      <li>
     *          <b>流程预览图：</b></br>
     *          <img src="https://cdn4.winhlb.com/2025/06/19/68537b093b460.png" >
     *      </li>
     *  </ul>
     **/
    @Test
    public void test() {
        Deployment deploy = repositoryService.createDeployment()
                .addClasspathResource("flow/revokeProcessInstanceProcess.bpmn20.xml")
                .name("流程撤销案例演示")
                .deploy();
        log.info("部署ID：{}", deploy.getId());
        //流程定义信息
        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
                .deploymentId(deploy.getId())
                .singleResult();
        log.info("流程定义ID：{}", processDefinition.getId());
        //设置流程发起人
        Authentication.setAuthenticatedUserId("张三");
        //启动流程实例
        ProcessInstance procInst = runtimeService.startProcessInstanceById(processDefinition.getId());
        log.info("流程实例ID：{}", procInst.getId());
        Authentication.setAuthenticatedUserId(null);
        TaskQuery taskQuery = taskService.createTaskQuery()
                .processInstanceId(procInst.getId());
        ExecutionQuery executionQuery = runtimeService.createExecutionQuery()
                .processInstanceId(procInst.getId());

        //查询特殊借款申请用户任务的task
        Task firstTask = taskQuery.singleResult();
        //完成特殊借款申请用户任务的task
        taskService.complete(firstTask.getId());
        log.info("完成用户任务：{}", firstTask.getName());
        List<Task> taskList1 = taskQuery.list();
        taskList1.stream().forEach(task -> {
            log.info("完成用户任务：{}", task.getName());
            taskService.complete(task.getId());
        });
        log.info("流程撤销前，当前任务为：{}", JSON.toJSONString(taskQuery.list(), taskFilter));
        Authentication.setAuthenticatedUserId("张三");
        //执行流程撤销操作
        revokeProcessService.executeRevoke(procInst.getId());
        log.info("流程撤销后，当前任务为：{}", JSON.toJSONString(taskQuery.list(), taskFilter));

        /**
         *  演示结果：
         **/
    }
}
